/**
 * Monitor special variables
 */
import { createProxy } from '../utils/util'
import Store from '../store/index'
import method from '../global/method'
import { toJson } from '../global/api'
import luckysheetConfigsetting from './luckysheetConfigsetting'

let undoTimer, redoTimer
function undoAccessible(len) {
  clearTimeout(undoTimer)
  undoTimer = setTimeout(() => {
    $('#luckysheet-icon-undo')[len ? 'removeClass' : 'addClass']('disabled')
  }, 10)
}
function redoAccessible(len) {
  clearTimeout(redoTimer)
  redoTimer = setTimeout(() => {
    $('#luckysheet-icon-redo')[len ? 'removeClass' : 'addClass']('disabled')
  }, 10)
}

export const initListener = function() {
  // createProxy(Store,['jfredo']);
  createProxy(Store, 'jfredo', (target, property, val, receiver) => {
    if (property !== 'length') {
      //  钩子函数
      method.createHookFunction('updated', val)
    }
    undoAccessible(Store.jfredo.length)
  })
  createProxy(Store, 'jfundo', (target, property, val, receiver) => {
    redoAccessible(Store.jfundo.length)
  })

  createProxy(Store, 'asyncLoad', (target, property, val, receiver) => {
    if (property === 'length' && val === 0) {
      method.createHookFunction('workbookCreateAfter', toJson())
    }
  })
}

export const domDestroyed = function() {
  $('#luckysheet-icon-morebtn-div').css('right', '-100px')
  luckysheetConfigsetting.hook.closeFnc()
  setTimeout(() => {
    $('#luckysheet-tooltip-up').css('top', '-100px')
    $('#luckysheet-icon-fillingRate').empty()
  }, 200)
}
